//
//  TitleTextFiled.m
//  GuiJuWo
//
//  Created by chenguibang on 16/3/26.
//  Copyright © 2016年 chenguibang. All rights reserved.
//

#import "TitleTextFiled.h"

@implementation TitleTextFiled

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.clearButtonMode = UITextFieldViewModeWhileEditing;
         UIToolbar * toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
        [toolBar setBarStyle:UIBarStyleDefault];
        
        UIBarButtonItem *SpaceButton=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                                   target:nil  action:nil]; // 让完成按钮显示在右侧
        
        UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"收起" style:UIBarButtonItemStylePlain  target:self action:@selector(takebackkeyboard)];
        doneButton.image = [UIImage imageNamed:@"rank"];
        NSArray * buttonsArray = [NSArray arrayWithObjects:SpaceButton,doneButton,nil];
        [toolBar setItems:buttonsArray];
        self.inputAccessoryView = toolBar;
        
    }
    return self;
}

-(void)setTitle:(NSString *)title{
    _title = title;
    self.titleLabel.text = title;
}

-(UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        [self addSubview:_titleLabel];
        _titleLabel.sd_layout.leftEqualToView(self).topEqualToView(self).bottomEqualToView(self).widthIs(70);
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        self.leftViewMode = UITextFieldViewModeAlways;
        self.leftView = _titleLabel;
        
        [[self rac_signalForControlEvents:UIControlEventEditingDidBegin] subscribeNext:^(id x){
            self.layer.shadowColor = [UIColor blackColor].CGColor;
            self.layer.shadowOffset = CGSizeMake(1, 1);
            self.layer.shadowOpacity = 0.3
            ;
            
        }];
        
        [[self rac_signalForControlEvents:UIControlEventEditingDidEnd] subscribeNext:^(id x){
            self.layer.shadowColor = [UIColor clearColor].CGColor;
            self.layer.shadowOffset = CGSizeMake(0, 0);
            self.layer.shadowOpacity = 0;
        }];
    }
    return _titleLabel;
}

-(void)takebackkeyboard{
    [self resignFirstResponder];
}


@end
